package com.gable.report.controller;

import com.alibaba.fastjson.JSON;
import com.gable.report.common.util.HttpHelper;
import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ParameterHandleView;
import com.gable.report.model.ParameterQuery;
import com.gable.report.model.User;
import com.gable.report.service.ParameterService;
import com.gable.report.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.gable.report.util.HttpProcessUtil.getAccessPath;
import static com.gable.report.util.HttpProcessUtil.headerList;

/**
 * @Author: 杨青桦
 * @Description:  运营管理界面参数接口
 * @Date: 2018/8/27 14:17
 */
@Controller
public class ChartsForHospitalOverviewParameterController {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    HttpServletRequest request;

    @Autowired
    ParameterService parameterService;

    @Value("${hospital.address}")
    String hospitalAddress;

    /**
     * @Author: 杨青桦
     * @Description: 查询事件基本设备信息
     * @Date: 2018/3/23 16:59
     */
    @RequestMapping(value="parameterEvent/handleAssetsInfo", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil handleAssetsInfo(ParameterHandleView handleView){
        ReturnResultUtil rru;
        try {
            rru = parameterService.queryParameterEventAssetsInfo(handleView);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return rru;
    }


    /**
     * @Author: 杨青桦
     * @Description: 参数实时监控图接口
     * @Date: 2018/3/23 16:59
     */
    @RequestMapping(value="parameter/live", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryLiveParameter(ParameterQuery query){
        Map<String,Object> returnMap;
        try {
            returnMap = parameterService.queryLiveParameter(query);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }

    }


    /**
     * @Author: 杨青桦
     * @Description:  设备参数(小时,天,月)图
     * @Date: 2018/8/28 13:02
     * @param query
     * @return
     */
    @RequestMapping(value="parameter/dimension", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryParameterCharts(ParameterQuery query){
        Map<String,Object> returnMap;
        try {
            returnMap = parameterService.queryParameterDimension(query);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",returnMap);
    }


    /**
     * @Author: 杨青桦
     * @Description:  参数异常事件处理
     * @Date: 2018/8/29 15:33
     * @param handleView
     * @return
     */
    @RequestMapping(value="parameter/handle", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil handle(ParameterHandleView handleView){
        ReturnResultUtil rru;
        try {
            User user = (User) request.getSession().getAttribute("loginUser");
            Long parameterEventId = handleView.getParameterEventId();
            Long status = handleView.getStatus();
            String handleTime = handleView.getHandleTime();
            String handleOpinion = handleView.getHandleOpinion();
            String handlePerson = handleView.getHandlePerson();
            String handleResult = handleView.getHandleResult();
            if(parameterEventId == null){
                return new ReturnResultUtil(0,"ID不能为空");
            }
            if(status == null){
                return new ReturnResultUtil(0,"状态不能为空");
            }
            if(status.intValue() == 38002){
                if(StringUtil.isEmpty(handleTime)){
                    return new ReturnResultUtil(0,"处理时间不能为空");
                }
                if(StringUtil.isEmpty(handleOpinion)){
                    return new ReturnResultUtil(0,"处理意见不能为空");
                }
                if(StringUtil.isEmpty(handlePerson)){
                    return new ReturnResultUtil(0,"处理人不能为空");
                }
                if(StringUtil.isEmpty(handleResult)){
                    return new ReturnResultUtil(0,"处理结果不能为空");
                }
            }
            handleView.setCreatePerson(user.getId());
            rru = parameterService.handle(handleView);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"处理异常");
        }
        return rru;
    }


    /**
     * @Author: 杨青桦
     * @Description: 全部实时参数接口(调用生产系统)
     * @Date: 2018/3/23 16:59
     */
    @RequestMapping(value="parameterEvent/queryAll", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAll(String assetsId,
                                     String parameterName,
                                     String startTime,
                                     String endTime,
                                     String parameterValue,
                                     String unitDetail,
                                     String createTime,
                                     String parameterLevel){
        String result;
        try {
            if(StringUtil.isEmpty(assetsId)){
                return new ReturnResultUtil(0,"参数错误");
            }
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("assetsId",assetsId);
            paramMap.put("parameterName",parameterName);
            paramMap.put("startTime",startTime);
            paramMap.put("endTime",endTime);
            paramMap.put("parameterValue",parameterValue);
            paramMap.put("unitDetail",unitDetail);
            paramMap.put("createTime",createTime);
            paramMap.put("parameterLevel",parameterLevel);
            String path = "parameterEvent/queryAll";    //接口路径
            String url = getAccessPath(hospitalAddress,path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList,url, paramMap);
            if(StringUtils.isEmpty(result)){
                new ReturnResultUtil(0, "网络异常",result);
            }
            return JSON.parseObject(result,ReturnResultUtil.class);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"网络异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 处理结果详情(调用生产系统)
     * @Date: 2018/3/23 16:59
     */
    @RequestMapping(value="parameterEvent/info", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAll(String parameterEventId,
                                     String status,
                                     String handleTypeText,
                                     String repairNumber,
                                     String repairId
                                     ){
        String result;
        try {
            if(StringUtil.isEmpty(parameterEventId)){
                return new ReturnResultUtil(0,"参数错误");
            }
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("parameterEventId",parameterEventId);
            paramMap.put("status",status);
            paramMap.put("handleTypeText",handleTypeText);
            paramMap.put("repairNumber",repairNumber);
            paramMap.put("repairId",repairId);
            String path = "parameterEvent/info";    //接口路径
            String url = getAccessPath(hospitalAddress,path); //访问完整路径
            result = HttpHelper.postRequestResponseBodyAsString(headerList,url, paramMap);
            if(StringUtils.isEmpty(result)){
                new ReturnResultUtil(0, "网络异常",result);
            }
            return JSON.parseObject(result,ReturnResultUtil.class);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"网络异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  其他异常参数列表
     * @Date: 2018/8/30 17:01
     * @param otherEventIds
     * @return
     */
    @RequestMapping(value="parameter/other", method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryOtherParameterInfoList(String otherEventIds){
        Map<String,Object> returnMap;
        try {
            returnMap = parameterService.queryOtherParameterInfoList(otherEventIds);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }

    }

}
